{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.0038479388314734"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "discount_rate = np.array([[4.41,4.72,5.3],\n",
    "                          [4.81,5.12,5.7],\n",
    "                          [5.51,5.92,6.3],\n",
    "                          [7.21,7.52,8.1],\n",
    "                          [11.71,13.52,15.1]\n",
    "                         ]) / 100 # 远期折现率\n",
    "\n",
    "value = []# 远期价值\n",
    "\n",
    "for i in range(5):\n",
    "    v = 100*0.05 + 100*0.05/(1+discount_rate[i,0]) + 100*0.05/(1+discount_rate[i,1]) ** 2+ 100*(1+0.05)/(1+discount_rate[i,2]) ** 3\n",
    "    value.append(v)\n",
    "    \n",
    "value = np.array(value)\n",
    "\n",
    "convert_rate = np.array([90.81, 8.33, 0.68, 0.06, 0.12]) / 100# 信用评级转移矩阵\n",
    "\n",
    "mean = sum(value * convert_rate)# 均值\n",
    "\n",
    "std = np.sqrt(sum(convert_rate * ((value - mean)**2)))# 标准差\n",
    "\n",
    "VaR = 2.33 * std\n",
    "\n",
    "VaR\n",
    "# Output：2.0038479388314734"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
